home *** CD-ROM | disk | FTP | other *** search
/ FM Towns: Free Software Collection 9 / FM Towns Free Software Collection 9.iso / t_os / shell / tsbgex / src / tool / setvar.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-16  |  4.7 KB  |  191 lines

  1. #include <stdio.h>
  2. #include "exp_hdr.h"
  3. #include "rex_hdr.h"
  4. #include "tmenu_ex.h"
  5. #include "hook.h"
  6.  
  7. #define TRUE    (1)
  8. #define FALSE    (0)
  9. #define VARLEN    12
  10.  
  11. char fname[256], line[80], var[16];
  12. char digit[] = "0123456789ABCDEF";
  13. struct rex_head hdr;
  14.  
  15. int link_head;
  16.  
  17. main(argc, argv)
  18. int argc;
  19. char *argv[];
  20. {
  21.     FILE *fp;
  22.     struct exp_head hdr;
  23.     struct node link;
  24.     long cur_node;
  25.     struct rex_head rhdr;
  26.     char *typestr, *valuestr;
  27.     int i, found, offset, base, type, value, nth;
  28.     extern char *strchr();
  29.  
  30.     if (argc != 5){
  31.         fprintf(stderr, "Usage: setvar EXG-file var type value\n");
  32.         exit(-1);
  33.     }
  34.     if (argc == 5){
  35.         strncpy(fname, argv[1], sizeof fname - 5);
  36.         strcat(fname, ".map");
  37.         strncpy(var, argv[2], VARLEN);
  38.         typestr = argv[3];
  39.         valuestr = argv[4];
  40.     } else {
  41.         strncpy(fname, argv[4], sizeof fname);
  42.         strncpy(var, argv[5], VARLEN);
  43.         typestr = argv[6];
  44.         valuestr = argv[7];
  45.         nth = atoi(argv[3]);
  46.     }
  47.     for (i = strlen(var); i < sizeof var; i++)
  48.         var[i] = ' ';
  49.     if (strcmp(typestr, "a") == 0) /* string */
  50.         type = 0;
  51.     else if (strcmp(typestr, "b") == 0) /* 1 byte */
  52.         type = 1;
  53.     else if (strcmp(typestr, "w") == 0) /* 2 byte */
  54.         type = 2;
  55.     else if (strcmp(typestr, "d") == 0) /* 4 byte */
  56.         type = 4;
  57.     else {
  58.         fprintf(stderr, "Error: invalid type %s\n", typestr);
  59.         exit(1);
  60.     }
  61.         
  62.     /* get offset of var */    
  63.     if ((fp = fopen(fname, "r")) == NULL){    /* open .MAP file */
  64.         perror(fname);
  65.         exit(-1);
  66.     }
  67.     found = FALSE;
  68.     while (fgets(line, sizeof line, fp) != NULL){
  69.         if (strncmp(line, "Public symbols", 14) == 0){
  70.             found = TRUE;
  71.             break;
  72.         }
  73.     }
  74.     if (!found){
  75.         fprintf(stderr, "Error: Public symbols not found.\n");
  76.         exit(-1);
  77.     }
  78.     fgets(line, sizeof line, fp);
  79.     fgets(line, sizeof line, fp);
  80.     fgets(line, sizeof line, fp);
  81.     found = FALSE;
  82.     while (fgets(line, sizeof line, fp) != NULL){
  83.         if (strncmp(line, var, VARLEN) == 0){
  84.             found = TRUE;
  85.             break;
  86.         }
  87.     }
  88.     fclose(fp);
  89.     if (!found){
  90.         var[VARLEN] = '\0';
  91.         fprintf(stderr, "Error: %s not found.\n", var);
  92.         exit(-1);
  93.     }
  94.     offset = 0;
  95.     for (i = 14; i < 22; i++){
  96.         offset *= 16;
  97.         offset += strchr(digit, line[i]) - digit;
  98.     }
  99.     
  100.     if (argc == 5){
  101.         strncpy(fname, argv[1], sizeof fname - 5);
  102.         strcat(fname, ".exg");
  103.         if ((fp = fopen(fname, "rb")) == NULL){
  104.             perror(fname);
  105.             exit(-1);
  106.         }
  107.         fread(&rhdr, REX_HEADER_SIZE, 1, fp);
  108.         if (!(rhdr.magic[0] == 'M'  &&  rhdr.magic[1] == 'Q' 
  109.              &&  rhdr.rel_off == 0x1e  &&  rhdr.overlay == 0
  110.              &&  rhdr.one == 1)){
  111.              fprintf(stderr, "Error: Can't handle this REX-header\n");
  112.              exit(1);
  113.         }
  114.         offset = (rhdr.header_para << 4) + offset;
  115.     } else {
  116.         strncpy(fname, argv[1], sizeof fname);
  117.         if ((fp = fopen(fname, "rb")) == NULL){
  118.             perror(argv[1]);
  119.             exit(-1);
  120.         }
  121.         fread(&hdr, sizeof hdr, 1, fp);
  122.         if (!(hdr.magic[0] == 'P'  &&  hdr.magic[1] == '3' 
  123.              &&  hdr.level == 1  &&  hdr.header_size == 0x0180
  124.              &&  hdr.runhead_off == 0x0180  &&  hdr.runhead_size == 0x80
  125.              &&  hdr.reloc_size == 0  &&  hdr.seginfo_size == 0  
  126.              &&  *(long *)hdr.image_off==0x0200 && *(long *)hdr.sym_size==0  
  127.              &&  *(long *)hdr.gdt_size == 0  && *(long *)hdr.ldt_size == 0
  128.              &&  *(long *)hdr.idt_size == 0  &&  *(long *)hdr.tss_size == 0
  129.              &&  *(long *)hdr.image_size == hdr.imagefile_size
  130.              &&  *(long *)hdr.image_size + *(long *)hdr.min_alloc == *(long *)hdr.esp)){
  131.              fprintf(stderr, "Error: Can't handle this header: %s\n", argv[1]);
  132.              exit(1);
  133.         }
  134.         
  135.         if ((link_head = pattern_search(fp, HEAD_ID, sizeof HEAD_ID)) <0){
  136.             fprintf(stderr, "Error: Can't find %s\n", HEAD_ID);
  137.             exit(1);
  138.         }
  139.         link_head -= (8 + *(long *)hdr.image_off);
  140.  
  141.         cur_node = link_head;
  142.         base = cur_node + *(long *)hdr.image_off;
  143.         fseek(fp, base, 0L);
  144.         fread(&link, sizeof link, 1, fp);
  145.         if (strncmp(link.id, HEAD_ID, 8) != 0){
  146.             fprintf(stderr, "Error: version error\n");
  147.             exit(1);
  148.         }
  149.         found = FALSE;
  150.       if (strcmp(HEAD_ID, argv[2]) == 0){
  151.           base -= 4;
  152.       } else {
  153.         while (link.next != 0  &&  !found){
  154.             base = link.next + *(long *)hdr.image_off;
  155.             fseek(fp, base, 0L);
  156.             cur_node = link.next;
  157.             fread(&link, sizeof link, 1, fp);
  158.             if (strncmp(link.id, argv[2], strlen(argv[2]))==0  &&  --nth == 0)
  159.                 found = TRUE;
  160.         }
  161.         if (!found){
  162.             fprintf(stderr, "Error: module %s not found.\n", argv[2]);
  163.             exit(-1);
  164.         }
  165.       }
  166.         offset = base + offset;
  167.     }
  168.     fclose(fp);
  169.     printf("offset = 0x%x\n", offset);
  170.     
  171.     /* set var */
  172.     if ((fp = fopen(fname, "rb+")) == NULL){
  173.         perror(fname);
  174.         exit(-1);
  175.     }
  176.     fseek(fp, offset, 0);
  177.     if (type == 0){    /* string */
  178.         fputs(valuestr, fp);
  179.         fputc('\0', fp);
  180.     } else { /* binary */
  181.         value = atoi(valuestr);
  182.         while (--type >= 0){
  183.             fputc(value & 0xff, fp);
  184.             value >>= 8;
  185.         }
  186.     }
  187.     fclose(fp);
  188.     
  189.     exit(0);
  190. }
  191.